package hzn;

import org.apache.zookeeper.*;

public class Servers {
    String url="hzn100:2181,hzn101:2181";
    int timeout=2000;
    ZooKeeper client;
    ZooKeeper getConnect()throws Exception{
        return new ZooKeeper(url, timeout, null);
    }

    //注册节点，节点中的数据为服务器ip
    void regist(String ip)throws Exception{
        client.create("/servers/server_"+ip+"_",ip.getBytes(), ZooDefs.Ids.OPEN_ACL_UNSAFE, CreateMode.EPHEMERAL_SEQUENTIAL);
        System.out.println(ip+"服务器上线了...");
    }

    public static void main(String[] args) throws Exception {
        Servers server=new Servers();
        server.client=server.getConnect();
        server.regist("192.168.184.100");
        Thread.sleep(Long.MAX_VALUE);
    }
}
